package com.allsheng.widget;

import java.text.SimpleDateFormat;

import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseApplication;

import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 *	更新的Dialog
 * @author Administrator
 *
 */
public class UpdateDialog extends Dialog {
	private Context context;
	private String title;
	private String message;
	private String rightText;
	private String scmText;
	private View view;
	RadioButton scm_up;
	RadioButton app_up;
	private UpdateClickListenerInterface clickListenerInterface;

	public UpdateDialog(Context context, String scmText) {
		super(context, R.style.dialog);
		this.scmText = scmText;
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	public void setClicklistener(UpdateClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	public interface UpdateClickListenerInterface {
		public void Cancle();

		public void OK();

		public void AppUpdate();

		public void SCMUpdate();

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.update_dialog, null);
		setContentView(view);
		setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
		TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
		TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
		TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
		scm_up = (RadioButton) view.findViewById(R.id.scm_up);
		scm_up.setText(scmText);
		app_up = (RadioButton) view.findViewById(R.id.app_up);

		app_up.setText("主程序升级，当前版本:V" + getVersion() + "," + "\r\n" + "时间:" + getCreateTime());
		tvMessage.setText(message);
		tvLeft.setOnClickListener(new clickListener());
		tvRight.setOnClickListener(new clickListener());
		scm_up.setOnClickListener(new clickListener());
		app_up.setOnClickListener(new clickListener());
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics();
		lp.width = (int) (d.widthPixels * 0.4);
		dialogWindow.setAttributes(lp);
	}

	private class clickListener implements View.OnClickListener {

		public void onClick(View v) {

			int id = v.getId();
			switch (id) {
			case R.id.tvBtnLeft:
				clickListenerInterface.Cancle();
				break;
			case R.id.tvBtnRight:
				clickListenerInterface.OK();
				break;
			case R.id.scm_up:
				clickListenerInterface.SCMUpdate();
				break;
			case R.id.app_up:
				clickListenerInterface.AppUpdate();
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public String getVersion() {
		try {
			PackageManager manager = BaseApplication.getContext().getPackageManager();
			PackageInfo info = manager.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "暂无";
		}
	}

	public String getCreateTime() {
		long firstInstallTime, lastUpdateTime = 0;
		try {
			PackageManager packageManager = BaseApplication.getContext().getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
			// 应用装时间
			firstInstallTime = packageInfo.firstInstallTime;
			// 应用最后一次更新时间
			lastUpdateTime = packageInfo.lastUpdateTime;
			// LogUtil.debug("first install time : " + firstInstallTime + " last
			// update time :" + lastUpdateTime);

		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		return df.format(lastUpdateTime);

	}
}
